<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@page import="java.util.*"%>
<%@page import="org.apache.jcs.admin.JCSAdminBean"%>
<%@page import="org.apache.jcs.admin.CacheElementInfo"%>
<%@page import="org.apache.jcs.admin.CacheRegionInfo"%>
<%@page import="org.apache.jcs.engine.control.CompositeCache"%>
<%@page import="org.apache.jcs.engine.memory.MemoryCache"%>
<%@page import="com.icss.ynt.common.util.*" %>
<!DOCTYPE HTML>
<html>
<head>
	<title>缓存管理</title>
	<style type="text/css">
		body {
			font: normal 11px auto "Trebuchet MS", Verdana, Arial, Helvetica,
				sans-serif;
			color: #4f6b72;
			background: #E6EAE9;
		}
		
		a {
			color: #c75f3e;
		}
		
		table {
			width: 100%;
			padding: 0;
			margin: 0;
		}
		
		caption {
			padding: 0 0 5px 0;
			width: 700px;
			font-size: 20px;
			text-align: right;
		}
		
		th {
			font: bold 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
			color: #4f6b72;
			border-right: 1px solid #C1DAD7;
			border-bottom: 1px solid #C1DAD7;
			border-top: 1px solid #C1DAD7;
			letter-spacing: 2px;
			text-transform: uppercase;
			text-align: left;
			padding: 6px 6px 6px 12px;
		}
		
		td {
			border-right: 1px solid #C1DAD7;
			border-bottom: 1px solid #C1DAD7;
			background: #fff;
			font-size: 11px;
			padding: 6px 6px 6px 12px;
			color: #4f6b72;
		}
		
		td.alt {
			background: #F5FAFA;
			color: #797268;
		}
		
		html>body td {
			font-size: 11px;
		}
	</style> 
</head>
<body  style="padding:10px;text-align:center;" >
<%
	try {
		StringBuffer sb = new StringBuffer();
		JCSAdminBean admin = new JCSAdminBean();

		String clearAll = request.getParameter("clearAll");
		if (!StringUtil.isEmpty(clearAll)) {
			admin.clearAllRegions();
		}
		String region = request.getParameter("region");
		if (!StringUtil.isEmpty(region)) {
			admin.clearRegion(region);
		}
		String cache = request.getParameter("cache");
		String key = request.getParameter("key");
		if (!StringUtil.isEmpty(key)) {
			admin.removeItem(cache, key);
		}
		@SuppressWarnings("rawtypes")
		LinkedList linkedList = admin.buildCacheInfo();
		@SuppressWarnings("rawtypes")
		ListIterator iterator = linkedList.listIterator();
		sb.append("<table id='cachetable' cellspacing='0'><tr>");
		sb.append("<caption>缓存区域清单</caption>");
		sb.append("<th>序号</th>");
		sb.append("<th>缓存名称</th>");
		sb.append("<th>缓存类型</th>");
		sb.append("<th>缓存大小</th>");
		sb.append("<th>缓存字节</th>");
		sb.append("<th>Cache Misses (not found)</th>");
		sb.append("<th>Cache Misses (expired)</th>");
		sb.append("<th>Cache Hits (memory)</th>");
		sb.append("<th>Cache Updates</th>");
		sb.append("<th>操作</th>");

		sb.append("</tr>");
		int size = 0;
		int bytesize = 0;
		int notfound = 0;
		int expired = 0;
		int hits = 0;
		int update = 0;
		int i = 1;
		String td = "<td>";
		while (iterator.hasNext()) {
			CacheRegionInfo info = (CacheRegionInfo) iterator.next();
			CompositeCache compCache = info.getCache();
			if (i % 2 == 0) {
				td = "<td class='alt'>";
			} else {
				td = "<td>";
			}
			sb.append("<tr>");
			sb.append(td).append(i).append("</td>");
			sb.append(td).append(compCache.getCacheName())
					.append("</td>");
			sb.append(td).append(compCache.getCacheType())
					.append("</td>");
			sb.append(td).append(compCache.getSize()).append("</td>");
			sb.append(td).append(info.getByteCount()).append("</td>");
			sb.append(td).append(compCache.getMissCountNotFound())
					.append("</td>");
			sb.append(td).append(compCache.getMissCountExpired())
					.append("</td>");
			sb.append(td).append(compCache.getHitCountRam())
					.append("</td>");
			sb.append(td).append(compCache.getUpdateCount())
					.append("</td>");
			sb.append(td).append(
					"<a href='cacheMgr.jsp?region="
							+ compCache.getCacheName() + "'>清除</a>");
			sb.append("&nbsp;&nbsp;&nbsp;&nbsp;<a href='cacheMgr.jsp?items="
					+ compCache.getCacheName() + "'>查看明细</a>");
			sb.append("</td>");
			sb.append("</tr>");

			size += compCache.getSize();
			bytesize += info.getByteCount();
			notfound += compCache.getMissCountNotFound();
			expired += compCache.getMissCountExpired();
			hits += compCache.getHitCountRam();
			update += compCache.getUpdateCount();
			++i;
		}

		if (i % 2 == 0) {
			td = "<td class='alt'>";
		} else {
			td = "<td>";
		}
		sb.append("<tr>");
		sb.append(td).append(i).append("</td>");
		sb.append(td).append("合计").append("</td>");
		sb.append(td).append("-").append("</td>");
		sb.append(td).append(size).append("</td>");
		sb.append(td).append(bytesize / 1024).append(" KB</td>");
		sb.append(td).append(notfound).append("</td>");
		sb.append(td).append(expired).append("</td>");
		sb.append(td).append(hits).append("</td>");
		sb.append(td).append(update).append("</td>");
		sb.append(td).append(
				"<a href='cacheMgr.jsp?clearAll=true'>清除全部</a></td>");
		sb.append("</tr>");

		sb.append("</table>");
		out.print(sb);

		//是否查看明细
		String items = request.getParameter("items");
		if (!StringUtil.isEmpty(items)) {
			sb.delete(0, sb.length());
			sb.append("<table id='itemstable' cellspacing='0'><tr>");
			sb.append("<caption>" + items + "缓存明细</caption>");
			sb.append("<th>序号</th>");
			sb.append("<th>Key</th>");
			sb.append("<th>创建时间</th>");
			sb.append("<th>生命周期（秒）</th>");
			sb.append("<th>失效时间（秒）</th>");
			sb.append("<th>操作</th>");
			sb.append("</tr>");
			i = 1;
			@SuppressWarnings("unchecked")
			LinkedList<CacheElementInfo> emlst = admin.buildElementInfo(items);
			ListIterator<CacheElementInfo> emit = emlst.listIterator();
			while (emit.hasNext()) {
				CacheElementInfo em = emit.next();
				if (i % 2 == 0) {
					td = "<td class='alt'>";
				} else {
					td = "<td>";
				}
				sb.append("<tr>");
				sb.append(td).append(i).append("</td>");
				sb.append(td).append(em.getKey()).append("</td>");
				sb.append(td).append(em.getCreateTime())
						.append("</td>");
				sb.append(td).append(em.getMaxLifeSeconds())
						.append("</td>");
				sb.append(td).append(em.getExpiresInSeconds())
						.append("</td>");
				sb.append(td).append(
						"<a href='cacheMgr.jsp?cache=" + items
								+ "&key=" + em.getKey() + "'>清除</a>");
				sb.append("</td>");
				sb.append("</tr>");
				++i;
			}
			sb.append("</table>");
			out.print(sb);
		}
	} catch (Exception e) {
		
	}
%>
</body>
</html>
